<%@ page import="com.scalar.chiptrack.operations.process.forms.GroupFabStageForm,
                 com.scalar.chiptrack.utils.ConfigManager,
                 java.util.ArrayList,
                 com.scalar.chiptrack.operations.process.FabStageGroup,
                 com.scalar.forecast.commons.Constants,
                 com.scalar.forecast.commons.SessionKeys" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld"    prefix="bean"      %>
<%@ taglib uri="/WEB-INF/struts-html.tld"    prefix="html"      %>
<%@ taglib uri="/WEB-INF/struts-logic.tld"   prefix="logic"     %>
<%@ taglib uri="/WEB-INF/struts-nested.tld"  prefix="nested"    %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>


<chiptrack:checkLogon sourcePage="/groupAssemblyStage.do" />

<% String chipTracContext = "/"+ConfigManager.getChipTracContext(); %>
<% User user1 = (User) session.getAttribute (SessionKeys.USER_KEY); %>
<HTML>
<HEAD>
<TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type="text/css" rel="stylesheet">
<script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>
<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
<script language="javascript">
var defaultStr = "";
function sendOptionElementLeftToRight ( index )
{
    var list1 = document.groupFabStageForm[ 'fabStage.fabStages[' + index +'].availableStages'];
    var list2 = document.groupFabStageForm[ 'fabStage.fabStages[' + index +'].selectedStages'];
    var noOfBoxesObj   = document.groupFabStageForm['fabStage.totalGroupNo'];
    if ( list_copySelected ( list1,list2, defaultStr ) > 0 )
    {
        for ( var i= 0; i <= noOfBoxesObj.value-1; i++ )
        {
            if( index != i ) {
                var list = document.groupFabStageForm[ 'fabStage.fabStages[' + i +'].availableStages'];
                list_deleteAll( list1, list, defaultStr );
            }
        }
        list_deleteSelected ( list1, defaultStr );
    }
}
function sendOptionElementRightToLeft ( index )
{
    var list1 = document.groupFabStageForm[ 'fabStage.fabStages[' + index +'].availableStages'];
    var list2 = document.groupFabStageForm[ 'fabStage.fabStages[' + index +'].selectedStages'];
    var noOfBoxesObj   = document.groupFabStageForm['fabStage.totalGroupNo'];

    for ( var i= 0; i <= noOfBoxesObj.value-1; i++ )
    {
        if( index != i ) {
            var list = document.groupFabStageForm[ 'fabStage.fabStages[' + i+'].availableStages'];
            list_copySelected( list2, list, defaultStr )
        }
    }
    if ( list_copySelected ( list2, list1, defaultStr ) > 0 )
    {
        list_deleteSelected ( list2, defaultStr );
    }
}

function list_copySelected(list1, list2, defaultStr)
{

    if ( list1 == null || list2 == null ){
        return -1;
    }
    else if ( list1.length == 1 && list1.options[0].selected && list1.options[0].text == defaultStr){
        return -2;
    }
    var copyCount = 0;

    // unselect all in list2 first (IE5 bug)
    for (var i = 0; i < list2.length; i++ ){
            list2.options[i].selected = false;
    }

    // copy the selected items from list1 to list2
    for (var i = 0; i < list1.length; i++ ){
        if ( list1.options[i].selected ){
            list2.options[list2.length] = new Option(list1.options[i].text, list1.options[i].value);
            copyCount++;
        }
    }

    // remove the default str from list2 if neccesary
    if ( list2.length > 1 && list2.options[0].text == defaultStr ){
        list2.options[0] = null;
    }

    return copyCount;
}
function list_deleteSelected(list1, defaultStr)
{
    // assertions
    if ( list1 == null ){
        return -1;
    }
    else if ( list1.length == 1 && list1.options[0].selected && list1.options[0].text == defaultStr){
        return -2;
    }

    // copy the selected items from list1 to the array
    // use trick with array because of IE5 bug
    var deleteCount = 0;
    var sel = new Array(list1.length);

    for (var i = 0; i < list1.length; i++ ){
        if ( list1.options[i].selected ){
            list1.options[i].selected = false;
            sel[deleteCount++] = i;
        }
    }

    for (var i = deleteCount - 1; i > -1 ; i-- ){
        list1.options[sel[i]] = null;
    }

    if ( list1.length == 0 )
        list1.options[0] = new Option(defaultStr, defaultStr);

    return deleteCount;
}

function list_deleteAll( list1, list, defaultStr )
{
    // assertions
    if ( list1 == null ){
        return -1;
    }
    else if ( list1.length == 1 && list1.options[0].selected && list1.options[0].text == defaultStr){
        return -2;
    }

    // copy the selected items from list1 to the array
    // use trick with array because of IE5 bug
    var deleteCount = 0;
    var sel = new Array(list1.length);

    for (var i = 0; i < list1.length; i++ ){
        if ( list1.options[i].selected ){
            sel[deleteCount++] = i;
        }
    }

    for (var i = deleteCount - 1; i > -1 ; i-- ){
        list.options[sel[i]] = null;
    }

    if ( list.length == 0 )
        list.options[0] = new Option(defaultStr, defaultStr);

    return deleteCount;
}
function addStage()
{
    var LeftPos = (screen.width)?(screen.width-500)/2:100;
    var TopPos  = (screen.height)?(screen.height-400)/2:100;
    var processStepId = document.groupFabStageForm['fabStage.processStepId'].value;
    newWindow = window.open ('<%=chipTracContext%>/groupAssemblyStage.do?action=addAssemblyStage&ProcessStepId='+processStepId, 'optionsPopup1', 'channelmode=no, directories=no, toolbar=no, menubar=no, personalbar=no, status=yes, scrollbars=yes, width=500, height=400, resizable=no Left= '+LeftPos+', top='+TopPos);
    newWindow.focus();
}
function loadPartNumbers()
{
    document.groupFabStageForm.action.value="loadPartNumbers"
    document.groupFabStageForm.submit();
}
function loadGroups()
{
    document.groupFabStageForm.action.value="loadGroups"
    document.groupFabStageForm.submit();
}
function loadStages()
{
    document.groupFabStageForm.action.value="loadStages"
    document.groupFabStageForm.submit();
}
function addMore()
{
    selectAll()
    document.groupFabStageForm.action.value="addMore"
    document.groupFabStageForm.submit();
}

var pa = '';
function saveStageGroups()
{
    pa = document.getElementById("message");
    var frm = document.groupFabStageForm['fabStage'];
    var noOfBoxesObj = document.groupFabStageForm['fabStage.totalGroupNo'];

    for( var i = 0 ; i < noOfBoxesObj.value ; i++ )
    {
        var selectedStageNameObject = document.groupFabStageForm[ 'fabStage.fabStages[' + i +'].selectedStages'];
        var groupNameTextObj = document.groupFabStageForm['fabStage.fabStages[' + i +'].groupName'];
        var groupId = document.groupFabStageForm['fabStage.fabStages[' + i +'].groupId'];

        for( var j = 0 ; j < selectedStageNameObject.length ; j++ )
        {
            selectedStageNameObject.options[j].selected = true;
        }

        if ( selectedStageNameObject.value == '' )
        {
            pa.innerHTML = "<li>Selected stages should not be null</li>";
            deSelectAll();
            return ;
        }
        else
        {
            if ( groupNameTextObj.value == '' )
            {
                pa.innerHTML = "<li>Enter The Group Name</li>";
                groupNameTextObj.focus();
                deSelectAll();
                return ;
            }

        }
        for( var j = i+1; j < noOfBoxesObj.value; j++ )
        {
           var nextgroupNameTextObj = document.groupFabStageForm['fabStage.fabStages[' + j +'].groupName'];
           if ( groupNameTextObj.value == nextgroupNameTextObj.value )
           {
                pa.innerHTML = "<li>Group Names should Not be Same</li>";
                nextgroupNameTextObj.focus();
                deSelectAll();
                return ;
           }
        }

    }
    document.groupFabStageForm.action.value="saveStageGroups";
    document.groupFabStageForm.submit();
}
function selectAll()
{
    var noOfBoxesObj = document.groupFabStageForm['fabStage.totalGroupNo'];
    if ( typeof  noOfBoxesObj != 'undefined' )
    {
        for( var i = 0 ; i < noOfBoxesObj.value ; i++ )
        {
            var selectedStageNameObject = document.groupFabStageForm[ 'fabStage.fabStages[' + i +'].selectedStages'];
            var availableStageNameObject = document.groupFabStageForm[ 'fabStage.fabStages[' + i +'].availableStages'];

            for ( var j = 0; j < selectedStageNameObject.length; j++ )
            {
                selectedStageNameObject.options[j].selected = true;
            }
            for ( var k = 0; k < availableStageNameObject.length; k++ )
            {
            availableStageNameObject.options[k].selected = true;
            }
        }
     }
}
function deSelectAll()
{
    var noOfBoxesObj = document.groupFabStageForm['fabStage.totalGroupNo'];
    if ( typeof  noOfBoxesObj != 'undefined' )
    {
        for( var i = 0 ; i < noOfBoxesObj.value ; i++ )
        {
            var selectedStageNameObject = document.groupFabStageForm[ 'fabStage.fabStages[' + i +'].selectedStages'];
            var availableStageNameObject = document.groupFabStageForm[ 'fabStage.fabStages[' + i +'].availableStages'];

            for ( var j = 0; j < selectedStageNameObject.length; j++ )
            {
                selectedStageNameObject.options[j].selected = false;
            }
            for ( var k = 0; k < availableStageNameObject.length; k++ )
            {
                availableStageNameObject.options[k].selected = false;
            }
        }
     }
}
function load()
{
    var disabled = '<%=session.getAttribute("disable")%>'
    var disableIsDefault = '<%=session.getAttribute("disableIsDefault")%>'
    if ( disabled == "true" )
    {
        if ( document.groupFabStageForm['fabStage.totalGroupNo'] != null )
        {
            document.groupFabStageForm['fabStage.totalGroupNo'].disabled = true
        }

    }
    if ( disableIsDefault == "true" )
    {
        if ( document.groupFabStageForm['fabStage.isDefault'] != null )
        {
            document.groupFabStageForm['fabStage.isDefault'].disabled = true
        }

    }
    deSelectAll();
}

</script>
</HEAD>
<BODY bgColor="#ffffff" leftMargin=0 topMargin=0 marginheight="0" marginwidth="0" onLoad="load()">
<!-- Top Header Start-->
<%@ include file="/jsp/includes/header.jsp" %>
<!-- Top Header End-->
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
    <TBODY>
    <TR>
	    <td width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>
    <!-- Side Navigation start -->
    <%@ include file="/jsp/operations/product_menu.jsp" %>
    <!-- end of the side navigations -->
    <TD bgcolor="#ffffff">
        <IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8>
    </TD>
    <TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
    <TD valign="top" width="100%">
    <IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
    <BR>
    <html:form name="groupFabStageForm" action="/groupAssemblyStage.do" method="post" type="com.scalar.chiptrack.operations.process.forms.GroupFabStageForm" scope="session">
    <nested:nest property = "fabStage" >
    <table border="0" width="90%" align=center>
        <tr><td colspan=5 class="NArialW" align="center">Group Assembly Stages</td></tr>
        <tr><td colspan=5>&nbsp;</td></tr>
        <tr><td colspan=5 class='NArialL'><strong><FONT id="message" styleClass="NArialL" color='FF0000'></FONT></td></tr>
        <tr><td colspan=5>
                <chiptrack:info styleClass="NArialL" />
                <chiptrack:errors styleClass="NArialL" />
            </td>
        </tr>
        <% if ( !user1.isActionAllowed ( Functionality.GROUP_ASSEMBLY_STAGES, User.USER_ACTION_MODIFY ) ) { %>
             <tr><td colspan=4 align='left'><font class='NArialS'><font color='blue'><b><%=Constants.NO_MODIFY_RIGHTS %></b></font></td></tr> <% } %>
        <tr><td colspan=5>&nbsp;</td></tr>
        <tr><td>
                <table border="0" width="90%">
                    <tr>
                        <td width="25%" class="NArialL" >Vendor Name:</td>
                        <td width="25%" class="NArialL">
                            <nested:select name="fabStage" property="locationId" styleClass="txfield" onchange="loadPartNumbers()" >
                                <html:option value="0">Select Vendor Name</html:option>
                                <html:options collection="vendorsList" property="value" labelProperty="label"/>
                            </nested:select>
                        </td>
                        <td width="2%">&nbsp;</td>
                      <logic:equal name="displayPartNumbers" value="true">
                        <td width="23%" class="NArialL" >Part Number:</td>
                        <td width="25%" align=left class="NArialL">
                            <nested:select name = "fabStage"  property="processStepId"   styleClass="txfield" onchange="loadGroups()"  >
                                <html:option value="0">Select PartNumber</html:option>
                                <html:options collection="partNumbersList" property="value" labelProperty="label"/>
                            </nested:select>
                        </td>
                      </logic:equal>
                      <logic:notEqual name="displayPartNumbers" value="true">
                        <td width="23%">&nbsp;</td>
                        <td width="25%">&nbsp;</td>
                      </logic:notEqual>
                    </tr>
                    <tr><td colspan="5" width="90%"><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></td></tr>
                    <logic:equal name="showGroupNumbers" value="true">
                    <tr>
                        <td width="25%" class="NArialL">&nbsp;</td>
                        <td width="25%" class="NArialL">&nbsp;</td>
                        <td width ="2%" class="NArialL">&nbsp;</td>
                        <td width ="23%" class="NArialL" align="right"><nested:checkbox name="fabStage" property="isDefault" styleClass="txfield"/></td>
                        <td width ="25%" class="NArialL">Set As Default</td>
                    </tr>
                    </logic:equal>
                    <tr>
                      <logic:equal name="showGroupNumbers" value="true">
                        <td width="35%" class="NArialL" >Select No. of Groups<font color= red>* </font></td>
                        <td width="10%" class="NArialL" >
                            <nested:select name = "fabStage"  property="totalGroupNo" styleClass="txfield"  onchange="loadStages()">
                                <html:option value="0">Groups</html:option>
                                <html:options collection="groupsList" property="value" labelProperty="label"/>
                            </nested:select>
                        </td>
                        <td width ="2%" class="NArialL">&nbsp;</td>
                       <logic:notEqual name="showStages" value="true" >
                        <td width ="23%" class="NArialL">&nbsp;</td>
                        <td width ="25%" class="NArialL">&nbsp;</td>
                       </logic:notEqual>
                      <logic:equal name="showStages" value="true">
                        <td width ="50%" colspan=2 class="NArialL">&nbsp;&nbsp;
                            <a class="leftsection" href="javascript:addStage()" onmouseover="window.status='Click here to add Assembly Stage Names';return true" onmouseout="window.status='';return true">Click here</a>&nbsp;
                        to add Stage Names</td>
                      </logic:equal>
                     </logic:equal>
                    </tr>
                    <tr><td colspan="5" width="90%"><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></td></tr>
                </table>
                <logic:equal name="showStages" value="true">
                <TABLE width="95%" border = "0">
                    <TR>
                        <TD class="NArialL" width="20%"> Available Stages :</TD>
                        <TD width="5%">&nbsp;</TD>
                        <TD class="NArialL" width="20%"> Selected Stages :</TD>
                        <TD width="30%">&nbsp;</TD>
                        <TD width="20%">&nbsp;</TD>
                    </TR>
                    <nested:iterate id="fabStage" property="fabStages" indexId= "stageItemid">
                    <TR>
                        <TD width="20%">
                            <nested:select  name="fabstageGroup" property="availableStages"  styleClass="emptyList" size="12" multiple = "true" >
                                 <html:options collection="availableStageList" property="value" labelProperty="label"/>
                            </nested:select>
                        </TD>
                        <TD width="5%">
                            <%
                                String jsFunctionStrLeft = "sendOptionElementLeftToRight(" + stageItemid + ");";
                                String jsFunctionStrRight = "sendOptionElementRightToLeft(" + stageItemid + ");";
                            %>
                            <input type="button" value=">>" onclick="<%=jsFunctionStrLeft%>">
                            <br>
                            <input type="button" value="<<"  onclick="<%=jsFunctionStrRight%>">
                        </TD>
                        <TD width="20%">
                            <nested:select name="fabstageGroup" property="selectedStages" styleClass="emptyList" size="12" multiple = "true" >
                                    <%  ArrayList selectedStageList = ((FabStageGroup)fabStage).getSelectedStagesList();
                                        pageContext.setAttribute("selectedStageList", selectedStageList );
                                        if ( ((FabStageGroup)fabStage).getIsNewGroup() ) {
                                            request.setAttribute( "showRemove" , "false");
                                        }
                                    %>
                                <html:options collection="selectedStageList" property="value" labelProperty="label"/>
                            </nested:select>
                        </TD>
                        <TD width="30%">
                            <TABLE border = "0">
                                <TR><TD class = "NArialL">Enter Group Name</TD></TR>
                                <TR><TD>&nbsp;</TD></TR>
                                <logic:equal name="showRemove" value="true">
                                <TR><TD class = "NArialL">Delete Group</TD></TR>
                                </logic:equal>
                            </TABLE>
                        </TD>
                        <TD width="20%">
                            <TABLE border = "0">
                                <TR>
                                    <TD>
                                        <nested:text name="fabstageGroup" property="groupName" size="15" maxlength="30" styleClass="txfield"/>
                                        <nested:hidden name="fabstageGroup" property="groupId"/>
                                    </TD>
                                </TR>
                                <TR><TD>&nbsp;</TD></TR>
                                <logic:equal name="showRemove" value="true">
                                <TR><TD><nested:checkbox name="fabstageGroup"  property ="checked" styleClass="txfield" /></TD></TR>
                                </logic:equal>
                            </TABLE>
                        </TD>
                    </TR>
                    </nested:iterate>
                    <TR><TD colspan="5" width="90%"><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></TD></TR>
                    <TR><TD align=right colspan=4><a href="javascript:addMore();"><img src="<%=chipTracContext%>/jsp/includes/images/addmore.gif" border="0" onmouseover="window.status='Click to Add Assembly Groups';return true" onmouseout="window.status='';return true" title="Click here to Add Assembly Groups" /></TD></TR>
                    <TR>
                        <TD align=center colspan=5>
                            <chiptrack:restrictedLink functionality='<%=Functionality.GROUP_ASSEMBLY_STAGES%>' action='<%=User.USER_ACTION_MODIFY%>' href="javascript:saveStageGroups();" onmouseover="window.status='Click here to save Assembly Groups';return true" onmouseout="window.status='';return true">
                                <img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" width="53" height="17"  onmouseover="window.status='Click here to save Assembly Groups';return true" onmouseout="window.status='';return true" title="Click here to save Assembly Groups" >
                            </chiptrack:restrictedLink>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <a href="<%=chipTracContext%>/groupAssemblyStage.do"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" width="53" height="17" onmouseover="window.status='Click to Cancel Changes';return true" onmouseout="window.status='';return true" title="Click here to Cancel Changes"></a>
                        </TD>
                    </TR>
                </TABLE>
                </logic:equal>
            </td>
        </tr>
    </table>

    </nested:nest>
    <input type="hidden" name="action">
    </html:form>
    </TD>
    <TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD>
</TR>
</TABLE>
<!-- Footer starts here -->
<%@ include file="/jsp/includes/footer.jsp" %>
<!-- footer end here -->
</BODY>
</HTML>